/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.mail;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.support.ResourceBundleMessageSource;
import po.Mail;
import po.User;
import service.MailService;
import service.NewsService;
import service.OrgService;

/**
 *
 * @author sony
 */
public class DeleteMailAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    private MailService mailService;
    private String message;
    private ResourceBundleMessageSource messageSource;

    public String execute() {
        if (ActionContext.getContext().getSession().get("user") == null) {
            message = messageSource.getMessage("authError", null, Locale.CHINA);
            return ERROR;
        } else {
            int userId = ((User) ActionContext.getContext().getSession().get("user")).getId();
            int mailId = Integer.parseInt(request.getParameter("mailId"));
            Mail mail = mailService.getMail(mailId);
            if (!mailService.isAuthor(mailId, userId)) {
                message = messageSource.getMessage("authError", null, Locale.CHINA);
                return ERROR;
            } else {
                if (userId == mail.getUserByToId().getId()) {
                    request.setAttribute("url", "receiveBox.action");
                    int state = mail.getIsread();
                    if (state%10 == 3) {
                        mailService.deleteMail(mailId);
                    } else {
                        mail.setIsread((state/10)*10+2);
                        mailService.updateMail(mail);
                    }
                } else if (userId == mail.getUserByFromId().getId()) {
                    int state = mail.getIsread();
                    request.setAttribute("url", "sendBox.action");
                    if (state%10== 2) {
                        mailService.deleteMail(mailId);
                    } else {
                        mail.setIsread((state/10)*10+3);
                        mailService.updateMail(mail);
                    }
                }

            }
        }
        return SUCCESS;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ResourceBundleMessageSource getMessageSource() {
        return messageSource;
    }

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public MailService getMailService() {
        return mailService;
    }

    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }
    
}
